package com.csdj.t309.controller;

import com.csdj.t309.entity.Cityarea;
import com.csdj.t309.entity.CityareaExample;
import com.csdj.t309.entity.Driverschool;
import com.csdj.t309.entity.DriverschoolExample;
import com.csdj.t309.mapper.CityareaMapper;
import com.csdj.t309.mapper.DriverschoolMapper;
import com.csdj.t309.service.CityareaService;
import com.csdj.t309.service.DriverschoolService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.List;

@Controller
public class DriverSchoolController {
    @Autowired
    private DriverschoolService driverschoolService;
    @Autowired
    private CityareaService cityareaService;


    @ResponseBody
    @RequestMapping("qqq")
    public String doUpload(@RequestParam(value = "file",required = false) MultipartFile[] file, HttpServletResponse response, HttpServletRequest request) throws UnsupportedEncodingException {
        for(MultipartFile f:file) {
            String filename = f.getOriginalFilename();
            String ext = filename.substring(filename.lastIndexOf("."));
            System.out.println(filename);
            long curTime = System.currentTimeMillis();
            try {
                Path path = Paths.get("d:", "upload");
                if (!Files.exists(path)) {
                    Files.createDirectories(path);
                }
                f.transferTo(Paths.get("d:", "upload", String.valueOf(curTime) + ext));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println();
           Cityarea cityarea=new Cityarea();
           cityarea.setName(request.getParameter("zh"));
           if(cityareaService.insert(cityarea)>0){
               return "true";
           }else {
               return "false";
           }

    }
    @ResponseBody
    @GetMapping("chaxun")
    public List chaxun(@RequestParam(required=true,defaultValue="1") String page,String id)   {
        List list1=new ArrayList();
        List list2=new ArrayList();
        DriverschoolExample driverschoolExample = new DriverschoolExample();
        if(id!=null&&!id.equals("")){
            driverschoolExample.createCriteria().andCityareaidEqualTo(Integer.parseInt(id));
        }
        PageHelper.startPage(Integer.parseInt(page), 3);
        List<Driverschool> list=driverschoolService.selectByExample(driverschoolExample);
        PageInfo<Driverschool> p = new PageInfo<>(list);
        CityareaExample cityareaExample=new CityareaExample();
        for (Driverschool driverschool:list){
            cityareaExample.createCriteria().andIdEqualTo(driverschool.getCityareaid());
            List<Cityarea> list3=cityareaService.selectByExample(cityareaExample);
            list2.add(list3.get(0).getName());
        }

        list1.add(list);
        list1.add(p);
        list1.add(list2);
        return list1;
    }
    @ResponseBody
    @GetMapping("xiugai")
    public String xiugai(@RequestParam(value = "id")String id,@RequestParam(value = "name")String name,
                       @RequestParam(value = "chengqu")String chengqu,@RequestParam(value = "address")String address,
                       @RequestParam(value = "phone")String phone)   {
       Driverschool driverschool=new Driverschool();
       driverschool.setId(Integer.parseInt(id));
       driverschool.setName(name);
       driverschool.setCityareaid(Integer.parseInt(chengqu));
       driverschool.setAddress(address);
       driverschool.setPhone(phone);
       if(driverschoolService.updateByPrimaryKeySelective(driverschool)>0){
           return "true";
       }else {
           return "false";
       }
    }
    @ResponseBody
    @GetMapping("cha")
    public Driverschool cha(String id)   {
        DriverschoolExample driverschoolExample = new DriverschoolExample();
            driverschoolExample.createCriteria().andIdEqualTo(Integer.parseInt(id));
        List<Driverschool> list=driverschoolService.selectByExample(driverschoolExample);
        return list.get(0);
    }
}
